package com.cms.recursoshumanos.component.base;

import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.TouchEvent;

public class CustomButtonColored extends CustomButtonField 
{
	
//	private Background backgroundColour = BackgroundFactory.createLinearGradientBackground(Color.LIGHTGRAY, Color.LIGHTGRAY, Color.GRAY, Color.GRAY);
//	private Background backgroundHightlight;
	
	public CustomButtonColored(String text, int highlightColour)
	{
		this(text, Field.FIELD_HCENTER, highlightColour);
	}
	
	public CustomButtonColored(String text, long style, int highlightColour)
	{
		super( text, RecursosHumanosScreen.normalButtonBorder, RecursosHumanosScreen.focusButtonBorder,  RecursosHumanosScreen.activeButtonBorder, 0x000000, 0xFFFFFF, 0xFFFFFF, style );
        setMargin( 5, 5, 5, 5 );
        
	}

	protected boolean keyChar( char character, int status, int time ) 
    {
        if( character == Characters.ENTER ) {
            clickButton();
            return true;
        }
        return super.keyChar( character, status, time );
    }
    
    protected boolean navigationClick( int status, int time ) 
    {
        clickButton(); 
        return true;    
    }
    
    protected boolean trackwheelClick( int status, int time )
    {        
        clickButton();    
        return true;
    }
    
    protected boolean invokeAction( int action ) 
    {
        switch( action ) {
            case ACTION_INVOKE: {
                clickButton(); 
                return true;
            }
        }
        return super.invokeAction( action );
    }    

    public void setDirty( boolean dirty ) {
        // We never want to be dirty or muddy
    }
     
    public void setMuddy( boolean muddy ) {
        // We never want to be dirty or muddy
    }
         
    /**
     * A public way to click this button
     */
    public void clickButton() 
    {
    
        fieldChangeNotify( 0 );
    }
    

  //#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0

      protected boolean touchEvent(TouchEvent message)
      {
          boolean isOutOfBounds = touchEventOutOfBounds( message );
          switch(message.getEvent()) {
              case TouchEvent.DOWN:
                  if( !isOutOfBounds ) {
                      if( !_active ) {
                          _active = true;
                          invalidate();
                      }
                      return true;
                  }
                  return false;
              case TouchEvent.UNCLICK:
                  if( _active ) {
                      _active = false;
                      invalidate();
                  }
                  
                  if( !isOutOfBounds ) {
                      clickButton();
                      return true;
                  }
              case TouchEvent.UP:
                  if( _active ) {
                      _active = false;
                      invalidate();
                  }
              
              default : 
                  return false;
          }
      }
      
      private boolean touchEventOutOfBounds( TouchEvent message )
      {
          int x = message.getX( 1 );
          int y = message.getY( 1 );
          return ( x < 0 || y < 0 || x > getWidth() || y > getHeight() );
      }
  //#endif
}

